<?php

namespace App\Http\Controllers\EmberApi;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Pool\DataReader;
use Illuminate\Support\Facades\DB;
use App\Pool\Statistics\Parser as StatisticsParser;
use App\Pool\Miners\Parser as MinersParser;
use App\Miners\Miner;
use Illuminate\Support\Facades\Redis;


class MinersController extends Controller
{
    //
    public function index(){
        $cache = Redis::get('xdagpoolminers');
        $stats = json_encode([]);
        if(!isset($cache)){
            $stats = $this->getData();
            Redis::setex('xdagpoolminers',60,$stats);
            $stats = json_decode($stats);
        }else{
            $stats = json_decode($cache);
        }
        return response()->json($stats);

    }

    protected function getData(){
        /*
          $dataReader = new DataReader();
          $stats = new StatisticsParser($dataReader->getLiveDataJson());
          $minersData = $dataReader->getFastDataJson();
          $minersArray = $minersData['miners']??[];
          $activeMiners = [];
          foreach ($minersArray as $miner) {
              if ($miner['address'] == 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'){
                  continue;
              }
              if (!in_array($miner['address'],$activeMiners) & $miner['status'] == 'active'){
                  $activeMiners[] = $miner['address'];
              }
          }
          //var_dump($activeMiners);
          $jsonArray = [];
          $jsonArray['hashrate'] = $stats->getPoolHashrate();
          $jsonArray['minersTotal'] = count($activeMiners);
          $jsonArray['now'] = strtotime(Carbon::now());
          foreach ($activeMiners as $activeMiner) {
              $minerStat = DB::table('miner_stats')
                  ->leftjoin('miners','miners.id','=','miner_stats.miner_id')
                  ->where('miners.address',$activeMiner)
                  ->select('miner_stats.*')
                  ->latest()
                  ->first();
              $jsonArray['miners'][$activeMiner]['hr'] = $minerStat->hashrate;
              $jsonArray['miners'][$activeMiner]['unpaidShares'] = $minerStat->unpaid_shares;
              $jsonArray['miners'][$activeMiner]['lastBeat'] = strtotime($minerStat->created_at);
              $jsonArray['miners'][$activeMiner]['offline'] = false;
          }
          */
        $dataReader = new DataReader();
        $stats = new StatisticsParser($dataReader->getLiveDataJson());

        $jsonArray = [];
        $jsonArray['hashrate'] = $stats->getPoolHashrate();
        $jsonArray['now'] = strtotime(Carbon::now());

        $activeMiners = DB::table('miners')
            ->where('status','active')
            ->get();
        $machinesTotal = 0;
        foreach ($activeMiners as $minerStat) {
            $minerAddress = $minerStat->address;
            //矿工当前算力
            $minerAddress = substr($minerAddress,0,5).'...'.substr($minerAddress,-5);
            $jsonArray['miners'][$minerAddress]['hr'] = $minerStat->hashrate;
            //矿工未支付贡献值
            $jsonArray['miners'][$minerAddress]['unpaidShares'] = $minerStat->unpaid_shares;

            $jsonArray['miners'][$minerAddress]['lastBeat'] = strtotime($minerStat->updated_at);
            $jsonArray['miners'][$minerAddress]['offline'] = false;
            $jsonArray['miners'][$minerAddress]['machinesCount'] = $minerStat->machines_count;
            $machinesTotal += $minerStat->machines_count;
        }
        $jsonArray['minersTotal'] = count($activeMiners);
        $jsonArray['machinesTotal'] = $machinesTotal;
        //return response()->json($jsonArray);
        return json_encode($jsonArray);

    }
}
